package jsyf.oa.service.report.tomorrowPlan;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.weekplan.TomorrowVisitDao;
import jsyf.oa.dao.wreport.WReportMainDao;
import jsyf.oa.entity.weekplan.TomorrowVisitPlanEntity;
import jsyf.oa.entity.wreport.WReportMainEntity;
import jsyf.oa.facade.weekplan.TomorrowPlanFacade;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by LT on 2018/5/14.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class TomorrowPlanService implements TomorrowPlanFacade {
    @Autowired
    private TomorrowVisitDao tomorrowVisitDao;
    @Autowired
    private WReportMainDao wReportMainDao;


    @Override
    public List<JSONObject> getByUserIdAndDateTime(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String dateTime = jsonParams.getString("dateTime");
        String date = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtils.isBlank(dateTime)) {
            try {
                date = DateUtils.getBeforeDate(sdf.parse(dateTime), 1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            date = DateUtils.getYesterday();
        }
        return tomorrowVisitDao.getTomorrowVisitToday(userId, date);
    }
    @Override
    public String add(String jsonData) {
        String wId="";
        List<TomorrowVisitPlanEntity> entity = (List<TomorrowVisitPlanEntity>) JSONArray.toCollection(JSONArray.fromObject(jsonData), TomorrowVisitPlanEntity.class);
        if (entity.size()>0){
            String userId= entity.get(0).getCreateUserId();
            String date = DateUtils.getYesterday();
            String reportDate=date.substring(0,10);
            if(!StringUtils.isBlank(userId)&&!StringUtils.isBlank(reportDate)){
                //删除第二天拜访计划
                tomorrowVisitDao.removeByCondition("AND CREATE_USER_ID=? AND REPORT_DATE=?",
                        new String[]{userId,reportDate});
                List<WReportMainEntity> wReportMainEntity=wReportMainDao.getByCondition("AND USER_ID=? AND REPORT_DATE=?",new String[]{userId,reportDate});
                if (wReportMainEntity.size()>0){
                    wId=wReportMainEntity.get(0).getId();
                }
                for (int i = 0; i < entity.size(); i++) {
                    TomorrowVisitPlanEntity model = entity.get(i);
                    model.setReportDate(model.getReportDate().substring(0,10));
                    model.setwId(wId);
                    if (!StringUtils.isBlank(model.getCustomerName())){
                        tomorrowVisitDao.add(model);
                    }

                }
            }
        }


        return "true";
    }

}
